<template>

    <el-container id="home">

        <!--侧边栏区域 begin -->
        <el-aside :width="asideWidth+'px'">
            <div class="aside-head">
                <el-avatar size="large"
                           src="https://i.loli.net/2021/04/25/ZevM5zkfqnPRs6o.png"></el-avatar>
                <span v-if="!collapse" style="margin-left: 15px;">丁昌江系统</span>
            </div>


            <!--侧边栏菜单区域 begin-->
            <el-menu router :collapse-transition="false"
                     background-color="#304156"
                     text-color="#fff"
                     unique-opened
                     :collapse="collapse"
                     @select="selectItem"
                     :default-active="currentIndex"
                     active-text-color="#409eff">

                <!--显示目录-->
                <el-submenu v-for="item in menuTree"
                            :index="item.id+''"
                            v-if="item.nodeType == '1'">
                    <template slot="title">
                        <i :class="item.iconUrl"></i>
                        <span>{{ item.name }}</span>
                    </template>

                    <!--显示二级菜单-->
                    <el-submenu v-for="subMenu in item.childMenu"
                                :index="subMenu.routerUrl"
                                v-if="subMenu.nodeType === '1'">
                        <template slot="title">
                            <i :class="subMenu.iconUrl"></i>
                            <span>{{ subMenu.name }}</span>
                        </template>

                        <el-menu-item v-for="page in subMenu.childMenu"
                                      v-if="subMenu.nodeType === '1'"
                                      :index="page.routerUrl">
                            <i :class="page.iconUrl"/>
                            <span>{{ page.name }}</span>
                        </el-menu-item>
                    </el-submenu>
                    <!--显示二级菜单-->

                    <!--显示三级菜单-->
                    <el-menu-item v-for="page in item.childMenu"
                                  v-if="page.nodeType ==='2'"
                                  :index="page.routerUrl">
                        <i :class="page.iconUrl"/>
                        <span>{{ page.name }}</span>
                    </el-menu-item>
                    <!--显示三级菜单-->

                </el-submenu>
            </el-menu>
            <!--侧边栏菜单区域 end-->

        </el-aside>
        <!--侧边栏区域 end -->


        <!--主体区域-->
        <el-main>
            <div class="main-head">
                <div class="main-head-nav">
                    <div class="main-head-nav-left">
                        <i @click="fold" class="iconfont icon-zhedie fold"></i>

                        <span style="color: #409eff">留空，加上自己的diy吧！</span>
                        <!--<el-breadcrumb separator=">">-->
                        <!--    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>-->
                        <!--    <el-breadcrumb-item>留空，换成需求吧！</el-breadcrumb-item>-->
                        <!--</el-breadcrumb>-->
                    </div>
                    <div class="main-head-nav-right">
                        <el-dropdown style="margin-top: 4px" @command="handleCommand" trigger="click">
                            <span style="display: flex;align-items: flex-end">
                                <img class="avatar" src="../assets/img/avatar.png"/>
                                <i class="el-icon-caret-bottom"></i>
                            </span>
                            <el-dropdown-menu slot="dropdown">
                                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </el-dropdown>
                    </div>
                </div>

                <router-tab v-if="isRouterAlive" restore />
            </div>

            <!--<router-view v-if="isRouterAlive"/>-->
        </el-main>
    </el-container>

</template>

<script>

import {mapState} from 'vuex'
import {resetRouter} from "../router";
import sessionApi from "../api/sessionApi";
export default {
    name: "Home",
    data() {
        return {
            collapse: false,
            asideWidth: (this.collapse?65:200),
            currentIndex: "migratePark",
            isRouterAlive: true,

        };
    },
    provide() {
        return {
            mainReload: this.reload
        }
    },
    created() {

        //this.$axiosApi.getMenuTree(null, (data) => {
        //    this.menuTree = data
        //})
    },
    //做成计算属性，这样vuex中的菜单数据改变时，菜单目录才能实时改变，
    //如果通过vuex赋值的写法，那么不会实时刷新
    computed: mapState({
        //authList，是vuex state维护的数据
        menuTree: "authList"
    }),
    methods: {
        //点击折叠
        fold() {
            this.collapse = !this.collapse;
            if (this.collapse === true) {
                this.asideWidth = 65;
            } else {
                this.asideWidth = 200;
            }
        },

        //导航项选择
        selectItem(index) {
            window.sessionStorage.setItem("currentIndex", index);
            this.currentIndex = index;
        },

        //处理下拉菜单指令
        handleCommand(command) {
            //退出登录
            if (command === "logout") {
                this.$post("/user/logout").then(() => {
                    this.$router.push({path: "/login"});
                });
            }
        },

        reload() {
            this.isRouterAlive = false
            this.$nextTick(() => {
                this.isRouterAlive = true
            })
        }
    }
}
</script>

<style scoped>

* {
    margin: 0;
    padding: 0;
}

.iconfont {
    padding-right: 10px;
    font-size: 20px;
}


#home {
    width: 100vw;
    height: 100vh;
}


.el-container {
    height: 100%;
    width: 100%;
    /*background-color: rgb(245, 246, 245);*/
}


/*左上角部位系统名词部位*/
.aside-head {
    height: 40px;
    width: 100%;
    background-color: #304156;
    color: white;
    text-align: center;
    padding: 20px 0;
    display: flex;
    justify-content: center;
    align-items: center;
}

/**
  * 主体头部:导航部分包括：面包屑和头像；下方是 tab 栏
  */
.main-head {
    height: 100px;
    width: 100%;
    background-color: #fff;
}

/*主体头部导航栏*/
.main-head-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    height: 60px;
    width: 100%;
    border-bottom: 1px solid rgba(0, 0, 0, .05);
    padding: 0 10px;
}

/*主体头部导航栏-左部*/
.main-head-nav-left {
    display: flex;
    align-items: center;
}

/*主体头部导航栏-右部*/
.main-head-nav-right {
    margin-right: 10px;
}

/*主体头部导航栏-头像*/
.main-head-nav-right .avatar {
    width: 50px;
    height: 50px;
    border-radius: 5px;
    margin-right: 8px;
    cursor: pointer;
}

/*主体头部导航栏-头像*/
.main-head-nav-right i {
    color: #606465;
    cursor: pointer;
    font-size: 10px;
}

/*折叠按钮*/
.fold {
    color: #304156;
    font-size: 25px;
    cursor: pointer;
}

.el-header img {
    height: 80%;
    border-radius: 50%;
    margin-right: 10px;
}

.el-aside {
    /*background-color: #545c64;*/
    background: linear-gradient(to bottom right, #304156, #304156);

}

/*/deep/ el-submenu__title{*/
/*  padding-left: 50px;*/
/*}*/


/*解决菜单栏右边黑线条问题*/
.el-menu {
    border-right: none;
}


/deep/ el-menu el-menu--inline {
    background-color: rgba(53, 60, 70, 1);
}


.el-main {
    margin: 0;
    padding: 0;
    /*background-color: rgb(242, 242, 242);*/
}

.el-dropdown-menu{
    width: 100px;
    text-align: center;
}


</style>
